using System;
using System.Collections.Generic;
using System.Text;
using System.IO;


using gudusoft.gsqlparser;
using gudusoft.gsqlparser.Units;

namespace removevars
{
    class Program
    {
        static void Main(string[] args)
        {
            TGSqlParser sqlparser = new TGSqlParser(TDbVendor.DbVMssql);
            
            sqlparser.Sqlfilename = args[0];
            int i = sqlparser.Parse();
            if (i == 0)
            {

                for (int j = 0; j < sqlparser.SqlStatements.Count(); j++)
                {

                    TLzCustomExpression whereclause = sqlparser.SqlStatements[j].WhereClause;
                    if (whereclause != null)
                    {
                        TSourceToken start = whereclause.StartToken;
                        TSourceToken end = whereclause.EndToken;
                        TSourceTokenList stlist = start.Container;
                        int t = 0;
                        for (int k = start.posinlist; k <= end.posinlist; k++)
                        {

                            if (stlist[k].TokenType == TTokenType.ttSqlVar)
                            {
                                t++;
                                if (t % 2 == 0)
                                {
                                    stlist[k].TokenStatus = TTokenStatus.tsDeleted;
                                }
                                else
                                {
                                    stlist[k].AsText = 'v'+k.ToString();
                                }
                            }
                        }

                        // rebuild the sql text
                        sqlparser.SqlStatements[j].ReBuildSql(TRebuildFlags.rfClearRemovedVars);
                        Console.WriteLine("");
                        Console.WriteLine(sqlparser.SqlStatements[j].AsText);
                    }
                }

            }
            else
                if (i == -1)
                {
                    Console.WriteLine("fatal error:" + sqlparser.ErrorMessages);
                }
                else
                    Console.WriteLine(sqlparser.ErrorMessages);

        }
    }
}
